home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte25 / ex5.c < prev    next >
C/C++ Source or Header  |  1995-04-23  |  3KB  |  78 lines

  1. #include <genstub.c>
  2.  
  3. // This is a child thread procedure that waits for a semaphore,
  4. // holds the semaphore for five seconds, and releases the semaphore.
  5. // Threads that cannot get semaphores will wait until other threads exit.
  6. DWORD WINAPI ChildThreadProc( HWND hWnd )
  7. {
  8.     TCHAR szBuffer[256];                            // buffer
  9.     DWORD dwSemCount = 0;                           // printing semaphore count
  10.     HANDLE hSemaphore = OpenSemaphore( SYNCHRONIZE, FALSE, "TEST_SEMAPHORE" );
  11.     wsprintf( szBuffer,"Thread %x waiting for semaphore %x",
  12.              GetCurrentThreadId( ), hSemaphore );
  13.     SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
  14.     // Check for signaled semaphore.
  15.     WaitForSingleObject( hSemaphore, INFINITE );
  16.     wsprintf( szBuffer,"Thread %x got semaphore", GetCurrentThreadId( ) );
  17.     SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
  18.     Sleep( 5000 );
  19.     // Release semaphore.
  20.     ReleaseSemaphore( hSemaphore, 1, &dwSemCount );
  21.     wsprintf( szBuffer,"Thread %x is done with semaphore. Its count was %ld.",
  22.             GetCurrentThreadId( ), dwSemCount );
  23.     SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
  24.     CloseHandle( hSemaphore );
  25.     ExitThread( TRUE );
  26. }
  27.  
  28. // Windows message procedure.
  29. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  30. {
  31.    static HANDLE hSemaphore = 0;
  32.  
  33.    switch (uMsg)
  34.    {
  35.            case WM_CREATE:
  36.                    hSemaphore = CreateSemaphore( NULL, 4, 4, "TEST_SEMAPHORE" );
  37.                    return DefWindowProc( hWnd, uMsg, wParam, lParam );
  38.            case WM_COMMAND:       // process menu items
  39.                    switch ( LOWORD( wParam )  )
  40.                    {
  41.                       case IDM_TEST:     // start up a thread.
  42.                         {
  43.                            DWORD dwChildId;
  44.                            CreateThread( NULL, 0, ChildThreadProc, hWnd, 0, &dwChildId );
  45.                         }
  46.                         break;
  47.                       case IDM_EXIT:
  48.                            DestroyWindow( hWnd );
  49.                            break;
  50.                    }
  51.            break;
  52.            case WM_USER:
  53.                    {  // Message to show synchronization actions.
  54.                       TCHAR szBuffer[101];
  55.                       static int row = 0;
  56.                       static int msg_num = 1;
  57.                       HDC hDC = GetDC( hWnd );
  58.                       FillMemory( szBuffer, 100, 32 );
  59.                       TextOut( hDC, 0, row, szBuffer, 100 );
  60.                       wsprintf( szBuffer, "%3d: %s", msg_num++, (LPTSTR)lParam );
  61.                       TextOut( hDC, 0, row, szBuffer, lstrlen( szBuffer ) );
  62.                       if ( row > 200 )
  63.                          row = 0;
  64.                       else
  65.                          row += 20;
  66.                       ReleaseDC( hWnd, hDC );
  67.                    }
  68.                    break;
  69.            case WM_DESTROY:
  70.                    if ( hSemaphore )
  71.                       CloseHandle( hSemaphore );
  72.                    PostQuitMessage( 0 );
  73.                    break;
  74.            default:
  75.                 return DefWindowProc( hWnd, uMsg, wParam, lParam );
  76.    }
  77.    return NULL;
  78. }